
  $(document).ready(function(){
	 $.ajax({
		 type:"post",
		 url:"showNotice.action",
		 success:function(data){
			 var json = $.parseJSON(data);
			 var result = $.parseJSON(json);
			 var allrow = result[0];
			 var pageSize = 10;
			 var count = parseInt(allrow % pageSize == 0 ? allrow / pageSize : allrow / pageSize + 1);
			// var pageindex = 1;
			 var container = document.getElementById("noticePage");
			// setPage(container, count, pageindex);
			 var noticeData = result[1];
			 var itableHtml = '';
			 for(var data in noticeData){
				 itableHtml += 
						'<tr style="cursor: pointer;">'+
							'<td style="width: 60%;">'+
								noticeData[data].title+
							'</td>'+
							'<td>'+
								noticeData[data].date+
							'</td>'+
							'<td>'+
								'<a href="javascript:void(0)" onclick="goEdit('+noticeData[data].id+')">编辑</a>|'+
								'<a href="javascript:void(0)" onclick="delNot('+noticeData[data].id+')">删除</a>'+
							'</td>';
			 }
			 $("#noticeTable").html(itableHtml);
			 goNoticePage(container, count,1);
		 }
	 });
 });
  
  function goNoticePage(container,count,pageindex){
	  	 var pageSize = 10;
	  	 
		 var itable = document.getElementById("noticeTable");
		 var num = itable.rows.length;//表格行数
		 
		 var startRow = (pageindex - 1) * pageSize;//开始显示的行数
		 var endRow = pageindex * pageSize;//结束显示的行数 
		 
		 endRow = (endRow > num)? num : endRow;
		 
//		 for(var i=0;i<1;i++){
//				var irow = itable.rows[i];
//				irow.style.display = "";
//			}
			
			for(var i=0;i<num;i++){
				var irow = itable.rows[i];
				if(i>=startRow&&i<endRow){
					irow.style.display = "";	
				}else{
					irow.style.display = "none";
				}
			}
			var a = [];
		    //总页数少于10 全部显示,大于10 显示前3 后3 中间3 其余....
		    if (pageindex == 1) {
		      a[a.length] = "<li><a href=\"#\">上一页</a></li>";
		    } else {
		      a[a.length] = "<li><a href=\"#\" onclick=\"goNoticePage("+(pageindex-1)+")\">上一页</a></li>";
		    }
		    function setPageList(i) {
		      if (pageindex == i) {
		        a[a.length] = "<li><a href=\"#\" class=\"on\" onclick=\"goNoticePage("+ i +")\">" + i + "</a></li>";
		      } else {
		        a[a.length] = "<li><a href=\"#\" onclick=\"goNoticePage("+i+")\">" + i + "</a></li>";
		      }
		    }
		    //总页数小于10
		    if (count <= 10) {
		      for (var i = 1; i <= count; i++) {
		        setPageList(i);
		      }
		    }
		    //总页数大于10页
		    else {
		      if (pageindex <= 4) {
		        for (var i = 1; i <= 5; i++) {
		          setPageList(i);
		        }
		      } else if (pageindex >= count - 3) {
		        for (var i = count - 4; i <= count; i++) {
		          setPageList(i);
		        }
		      }
		      else { //当前页在中间部分
		        for (var i = pageindex - 2; i <= pageindex + 2; i++) {
		          setPageList(i);
		        }
		      }
		    }
		    if (pageindex == count) {
		      a[a.length] = "<li><a href=\"#\">下一页</a></li>";
		    } else {
		      a[a.length] = "<li><a href=\"#\" onclick=\"goNoticePage("+(pageindex+1)+")\">下一页</a><li>";
		    }
		    container.innerHTML = a.join("");
		    
		    var oAlink = container.getElementsByTagName("a");
		      var inx = pageindex; //初始的页码
		      oAlink[0].onclick = function() { //点击上一页
		        if (inx == 1) {
		          return false;
		        }
		        inx--;
		        goNoticePage(container, count, inx);
		        return false;
		      };
		      for (var i = 1; i < oAlink.length - 1; i++) { //点击页码
		        oAlink[i].onclick = function() {
		          inx = parseInt(this.innerHTML);
		          goNoticePage(container, count, inx);
		          return false;
		        };
		      }
		      oAlink[oAlink.length - 1].onclick = function() { //点击下一页
		        if (inx == count) {
		          return false;
		        }
		        inx++;
		        goNoticePage(container, count, inx);
		        return false;
		      };
  }
  
  	function goAdd(){
		var addHtml = 
			'<div class="container">'+
				'<input type="text" value="请输入标题" id="newTitle" style="height: 40px;width: 100%"/>'+
				'<textarea rows="30" cols="50" name="editor01" id="newContent">请输入内容</textarea>'+
				'<script type="text/javascript">var editor=CKEDITOR.replace(\'editor01\');</script>'+
				'<div style="float: right;margin-top: 10px"><a href="javascript:void(0)" onclick="addNotice()">提交</a>&nbsp;&nbsp<a href="javascript:void(0)" onclick="window.location.reload();">返回</a></div>'+
			'</div>';
		$("#tab3").html(addHtml);
	}
  	
  	function addNotice(){
  		if($("#newTitle").val().trim()==null||$("#newTitle").val().trim()==""){
  			alert("请输入标题");
  			$("#newTitle").val("请输入标题");
  			return;
  		}
  		if(editor.getData().trim()==null||editor.getData().trim()==""){
  			alert("请输入内容");
  			editor.setData("请输入内容");
  			return;
  		}
  		var newTitle = $("#newTitle").val().trim();
  		var newContent = editor.getData();
  		$.ajax({
  			type:'post',
  			url:'addNotice.action',
  			data:'notice.title='+newTitle+'&notice.content='+newContent,
  			success:function(data){
  				if(data=="\"got\""){
  					alert("添加成功");
  	  				window.location.reload();
  				}else{
  					alert("由于未知原因添加失败");
  					window.location.reload();
  				}
  			}
  		});
  	}
  	
  	function goEdit(id){
  		$.ajax({
  			type:'post',
  			url:'showNoticeOne.action',
  			data:'notice.id='+id,
  			success:function(data){
  				var json = $.parseJSON(data);
  				var result = $.parseJSON(json);
  				var editHtml = 
  					'<div class="container">'+
  						'<input type="text" value='+result.title+' id="editTitle" style="height: 40px;width: 100%"/>'+
  						'<textarea rows="30" cols="50" name="editor02" id="editContent"></textarea>'+
  						'<script type="text/javascript">var editor=CKEDITOR.replace(\'editor02\');</script>'+
  						'<div style="float: right;margin-top: 10px"><a href="javascript:void(0)" onclick="updateNotice('+result.id+')">保存</a>&nbsp;&nbsp<a href="javascript:void(0)" onclick="window.location.reload();">返回</a></div>'+
  					'</div>';
  				$("#tab3").html(editHtml);
  				editor.setData(result.content);
  			}
  		});
  	}

  	function updateNotice(id){
  		if(confirm("确定修改？")){
  			if($("#editTitle").val().trim()==null||$("#editTitle").val().trim()==""){
  	  			alert("请输入标题");
  	  			$("#editTitle").val("请输入标题");
  	  			return;
  	  		}
  	  		if(editor.getData().trim()==null||editor.getData().trim()==""){
  	  			alert("请输入内容");
  	  			editor.setData("请输入内容");
  	  			return;
  	  		}
  			var title = $("#editTitle").val().trim();
  			var content = editor.getData();
  			$.ajax({
  				type:'post',
  	  			url:'updateNotice.action',
  	  			data:'notice.id='+id+'&notice.title='+title+'&notice.content='+content,
  	  			success:function(data){
  	  				if(data=="\"edit\""){
  	  					alert("修改成功");
  	  					window.location.reload();
  	  				}else{
  	  					alert("由于未知原因修改失败");
  	  					window.location.reload();
  	  				}
  	  			}
  			});
  		}else{
  			return;
  		}
  	}
  	
  	function delNot(id){
  		if(confirm("确定删除？")){
  			$.ajax({
  				type:'post',
  	  			url:'delNotice.action',
  	  			data:'notice.id='+id,
  	  			success:function(data){
  	  				if(data=="\"del\""){
  	  					alert("已删除");
  	  					window.location.reload();
  	  				}else{
  	  					alert("由于未知原因删除失败");
  	  					window.location.reload();
  	  				}
  	  			}
  			});
  		}else{
  			return;
  		}
  	}
  